﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DacSanMienTayOBJ;
using System.Security.Cryptography;
using System.Text;
public partial class UserControl_ManagerProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void AddProduct()
    {
        try
        {
            string productName = Request["txtProductName"];
            string productGuide = Request["txtProductOriginal"];
            string productOriginal = Request["txtProductGuide"];
            float originalPrice = 0;
            float realPrice = 0;
            int districtID = 0;
            int quantity = 0;
            try
            {
                originalPrice = float.Parse(Request["txtOriginalPrice"]);
            }
            catch
            {

            }
            try
            {
                realPrice = float.Parse(Request["txtRealPrice"]);
            }
            catch
            {

            }
            try
            {
                quantity = int.Parse(Request["txtQuantity"]);
            }
            catch
            {

            }
            try
            {
                districtID = int.Parse(Request["cbDistrict"]);
            }
            catch
            {

            }
            string measurement = Request["txtMeasurement"];

            string fileUpload = "";
            Random r = new Random();
            string s = r.Next(100000).ToString() + DateTime.Now.ToString() + "" + DateTime.Now.Ticks.ToString();
            if (fileImage.FileName != "")
            {
                string img = s + fileImage.FileName;
                img = EncodeString(img) + ".jpg";
                string image = Server.MapPath("~/ProductImages/" + img);
                fileImage.PostedFile.SaveAs(image);
                fileUpload = "ProductImages/" + img;
            }
            string metakey = Request["txtMetakey"];
            string description = Request["txtDescription"];

            Product product = new Product();
            product.AdminID = 1;
            product.ProductName = productName;
            product.RealPrice = realPrice;
            product.OriginalPrice = originalPrice;
            product.Quantity = quantity;
            product.ProductImage = fileUpload;
            product.ProductOriginal = productOriginal;
            product.ProductGuide = productGuide;
            product.Priority = 1;
            product.IsVisble = 1;
            product.CreateDate = DateTime.Now;
            product.ExpireDate = DateTime.Now;
            product.ContentProduct = description;
            product.MeasurementUnits = measurement;
            product.Metakeyword = metakey;
            bool rs = ProductBusiness.InsertProduct(product, ConnectionType.AdminConnection);
            if (rs)
            {
                int productNewId = ProductBusiness.GetProductIDNewest(ConnectionType.AdminConnection);
              
                bool rsDistrict = ProductBusiness.InsertProductDistrict(productNewId, districtID, ConnectionType.AdminConnection);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã thêm sản phẩm mới')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi khi thêm sản phẩm. Vui lòng liên hệ admin')", true);
            }
        }
        catch
        {

        }
    }

    private string EncodeString(string value)
    {
        Byte[] originalBytes;
        Byte[] encodedBytes;
        MD5 md5;
        md5 = new MD5CryptoServiceProvider();
        originalBytes = ASCIIEncoding.Default.GetBytes(value);
        encodedBytes = md5.ComputeHash(originalBytes);
        return BitConverter.ToString(encodedBytes);
    }

    protected void cmdAddProduct_Click(object sender, EventArgs e)
    {
        AddProduct();
    }

    private void UpdateProduct()
    {
        try
        {
            int productID = int.Parse(Request["hdProductID"]);
            string productName = Request["txtProductName"];
            string productGuide = Request["txtProductOriginal"];
            string productOriginal = Request["txtProductGuide"];
            float originalPrice = 0;
            float realPrice = 0;
            int districtID = 0;
            int quantity = 0;
            try
            {
                originalPrice = float.Parse(Request["txtOriginalPrice"]);
            }
            catch
            {

            }
            try
            {
                realPrice = float.Parse(Request["txtRealPrice"]);
            }
            catch
            {

            }
            try
            {
                quantity = int.Parse(Request["txtQuantity"]);
            }
            catch
            {

            }
            //try
            //{
            //    districtID = int.Parse(Request["cbDistrict"]);
            //}
            //catch
            //{

            //}
            string measurement = Request["txtMeasurement"];

            string oldImage = Request["hdProductImage"];

            string fileUpload = oldImage;
            Random r = new Random();
            string s = r.Next(100000).ToString() + DateTime.Now.ToString() + "" + DateTime.Now.Ticks.ToString();
            if (fileImage.FileName != "")
            {
                string img = s + fileImage.FileName;
                img = EncodeString(img) + ".jpg";
                string image = Server.MapPath("~/ProductImages/" + img);
                fileImage.PostedFile.SaveAs(image);
                fileUpload = "ProductImages/" + img;
            }
            string metakey = Request["txtMetakey"];
            string description = Request["txtDescription"];

            Product product = new Product();
            product.AdminID = 1;
            product.ProductName = productName;
            product.RealPrice = realPrice;
            product.OriginalPrice = originalPrice;
            product.Quantity = quantity;
            product.ProductImage = fileUpload;
            product.ProductOriginal = productOriginal;
            product.ProductGuide = productGuide;
            product.Priority = 1;
            product.IsVisble = 1;
            product.CreateDate = DateTime.Now;
            product.ExpireDate = DateTime.Now;
            product.ContentProduct = description;
            product.MeasurementUnits = measurement;
            product.Metakeyword = metakey;
            bool rs = ProductBusiness.UpdateProduct(productID,product, ConnectionType.AdminConnection);
            if (rs)
            {
               
               
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã cập nhật sản phẩm')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi khi cập nhật sản phẩm. Vui lòng liên hệ admin')", true);
            }
        }
        catch
        {

        }
    }
    protected void cmdUpdateProduct_Click(object sender, EventArgs e)
    {
        UpdateProduct();
    }
}